<?php
require_once ($_SERVER["DOCUMENT_ROOT"] ."/include/config.php");
require_once ("sql.php");

class SplitterController extends BaseController {

	static public function GetSplitterTypesArray() {
		self::SQL_connect();
		$cables = self::GetSplitterTypesList(false);
		self::SQL_close();
		return $cables;
	}

	static public function ProcessRequest($request) {
		self::SQL_connect();

		if ($request == "list")
			self::GetSplitterTypesList();
		else if ($request == "update_type") {
				if (isset($_POST["data"]))
					self::UpdateSplitterTypes($_POST["data"]);
		}
        else if($request == "get_splitter_info" && isset($_POST["id"]))
        {
            self::GetSplitterInfo($_POST["id"]);
        }
        else if($request == "get_splitter_type_info" && isset($_POST["id"]))
        {
            self::GetSplitterTypeInfo($_POST["id"]);
        }
		else if (isset($_POST["id"])) {
			$id = $_POST["id"];
			if ($request == "get_type")
				self::GetSplitterType($id);
			else if ($request == "del_type") {
				if (isset($_POST["id"]))
					self::DeleteSplitterType($id);
			} 
		}
		
		self::SQL_close();
	}

    static private function GetSplitterTypeInfo($id)
    {
        $query1 = "SELECT * FROM splitters_types WHERE id = '$id';";
        $result1 = self::$db_link -> query($query1);
        $splitterInfo = $result1->fetch_object();

        $values = array();
        $query2 = "select * from splitters_attenuation where id = '$id' order by output_number;";
        $result2 = self::$db_link -> query($query2);
        while ($row = $result2 -> fetch_object())
            $values[] = $row;
        $result2 -> close();

        echo json_encode((object)array("splitter" => $splitterInfo, "attenuations" => $values));
    }

	static private function GetSplitterInfo($id)
    {
        $query1 = "SELECT splitters.id, splitters_types.attenuation, splitters.serial, splitters_types.id AS typeID, splitters_types.title AS typeTitle, splitters_types.inputs, splitters_types.outputs from splitters " .
            "LEFT JOIN splitters_types ON splitters_types.id = splitters.id_type where splitters.id = '$id';";
        $result1 = self::$db_link -> query($query1);
        $splitterInfo = $result1->fetch_object();

        $values = array();
        $query2 = "select * from splitters_attenuation where id = '$splitterInfo->typeID' order by output_number;";
        $result2 = self::$db_link -> query($query2);
        while ($row = $result2 -> fetch_object())
            $values[] = $row;
        $result2 -> close();

        echo json_encode((object)array("splitter" => $splitterInfo, "attenuations" => $values));
    }

	static private function GetSplitterType($id) {
		$query = "SELECT * FROM splitters_types WHERE id = '$id';"; 
		$result = self::$db_link -> query($query);
		if ($row = $result->fetch_object())
			echo json_encode($row);
	}

	static private function GetSplitterTypesList($echo = true) {

		$query = "SELECT * FROM splitters_types ORDER BY title; ";
		$splitters = Array();
		$result = self::$db_link -> query($query);
		while ($row = $result -> fetch_object())
			$splitters[] = $row;
		$result -> close();

		if ($echo)
			echo json_encode($splitters);
		return $splitters;
	}

	static private function DeleteSplitterType($id) {
		$query = "delete from splitters_attenuation where id = '$id'; DELETE FROM splitters_types WHERE id = '$id';";
		self::ProcessQuery($query);
	}

	static private function UpdateSplitterTypes($_data) {
		$data = json_decode($_data);

        $title = mysqli_real_escape_string( self::$db_link,trim($data->title));
        $inputs = mysqli_real_escape_string( self::$db_link,trim($data->inputs));
        $outputs = mysqli_real_escape_string( self::$db_link,trim($data->outputs));
        $attenuation = $data->attenuation == null ? "NULL" :  mysqli_real_escape_string( self::$db_link,trim($data->attenuation));

		$query = "delete from splitters_attenuation where id = '$data->id';";
        $query .= "INSERT INTO splitters_types (id, title, inputs, outputs, attenuation) VALUES ('$data->id', '$title', $inputs, $outputs, $attenuation)
			ON DUPLICATE KEY UPDATE title = '$title', inputs = $inputs, outputs = $outputs, attenuation = $attenuation;";

        if($data->attenuationValues != null)
        {
            foreach($data->attenuationValues as $row)
            {
                $number = mysqli_real_escape_string( self::$db_link,trim($row->output_number));
                $value = mysqli_real_escape_string( self::$db_link,trim($row->attenuation));
                $query .= "INSERT INTO splitters_attenuation (id, output_number, attenuation) VALUES ('$data->id', $number, $value);";
            }
        }

		self::ProcessQuery($query);
	}
}

SplitterController::$config = $config;
if (isset($_POST["op"])) 
	SplitterController::ProcessRequest($_POST["op"]);

?>